package com.nest.WorkNest.act;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.nest.WorkNest.R;
import com.nest.WorkNest.adapter.AdPageAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class NavigationAct extends BaseActivity {
    /** Called when the activity is first created. */
    private ViewPager adViewPager;
    private LinearLayout pagerLayout;
    private List<View> pageViews;
    private ImageView[] imageViews;
    private ImageView imageView;
    private AdPageAdapter adapter;
    private AtomicInteger atomicInteger = new AtomicInteger(0);
    private Context context = NavigationAct.this;
    private Button startbtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        hideStatusBar();
        setContentView(R.layout.act_navigation);
        startbtn = (Button) findViewById(R.id.startBtn);
        initViewPager();
    }

    private void initViewPager() {

        // 从布局文件中获取ViewPager父容器
        pagerLayout = (LinearLayout) findViewById(R.id.ll_navigation_content);
        // 创建ViewPager
        adViewPager = new ViewPager(this);

        // 获取屏幕像素相关信息
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        // 根据屏幕信息设置ViewPager广告容器的宽高
        adViewPager.setLayoutParams(new LayoutParams(dm.widthPixels,
                dm.heightPixels));

        // 将ViewPager容器设置到布局文件父容器中
        pagerLayout.addView(adViewPager);

        initPageAdapter();

        initCirclePoint();

        adViewPager.setAdapter(adapter);
        adViewPager.setOnPageChangeListener(new AdPageChangeListener());

    }

    private void initPageAdapter() {
        pageViews = new ArrayList<View>();

        ImageView img1 = new ImageView(this);
        img1.setBackgroundResource(R.drawable.page2_navigation);
        pageViews.add(img1);

        ImageView img2 = new ImageView(this);
        img2.setBackgroundResource(R.drawable.page1_navigation);
        pageViews.add(img2);

        ImageView img3 = new ImageView(this);
        img3.setBackgroundResource(R.drawable.page3_navigation);
        pageViews.add(img3);

        adapter = new AdPageAdapter(pageViews);
    }

    private void initCirclePoint() {
        ViewGroup group = (ViewGroup) findViewById(R.id.ll_navigation_viewGroup);
        imageViews = new ImageView[pageViews.size()];
        // 广告栏的小圆点图标
        for (int i = 0; i < pageViews.size(); i++) {
            // 创建一个ImageView, 并设置宽高. 将该对象放入到数组中
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    30, 30);
            imageView = new ImageView(this);
            params.setMargins(10, 0, 10, 0);
            imageView.setLayoutParams(params);

            imageViews[i] = imageView;

            // 初始值, 默认第0个选中
            if (i == 0) {
                imageViews[i].setBackgroundResource(R.drawable.point_focused);
            } else {
                imageViews[i].setBackgroundResource(R.drawable.point_unfocused);
            }
            // 将小圆点放入到布局中
            group.addView(imageViews[i]);
        }
    }

    /**
     * ViewPager 页面改变监听器
     */
    private class AdPageChangeListener implements OnPageChangeListener {

        /**
         * 页面滚动状态发生改变的时候触发
         */
        @Override
        public void onPageScrollStateChanged(int arg0) {
        }

        /**
         * 页面滚动的时候触发
         */
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        /**
         * 页面选中的时候触发
         */
        @Override
        public void onPageSelected(int arg0) {
            // 获取当前显示的页面是哪个页面
            atomicInteger.getAndSet(arg0);
            // 重新设置原点布局集合 c
            for (int i = 0; i < imageViews.length; i++) {
                imageViews[arg0]
                        .setBackgroundResource(R.drawable.point_focused);
                if (arg0 != i) {
                    imageViews[i]
                            .setBackgroundResource(R.drawable.point_unfocused);
                }
                if (arg0 == 2) {
                    startbtn.setVisibility(View.VISIBLE);
                } else {
                    startbtn.setVisibility(View.GONE);
                }
            }
        }

    }

    private void hideStatusBar() {
        WindowManager.LayoutParams attrs = getWindow().getAttributes();
        attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
        getWindow().setAttributes(attrs);
    }

    public void startbutton(View v) {
        Intent intent = new Intent();
        intent.setClass(context, LoginAct.class);
        startActivity(intent);
        this.finish();
    }
}